package com.server.game.config;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import net.sf.ehcache.CacheManager;

import org.apache.commons.io.FileUtils;

import redis.clients.jedis.JedisPool;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSON;
import com.sandking.config.SK_Config;
import com.sandking.tools.SK_Map;

public class GameConfig {
	static DruidDataSource logicDataSource;
	static DruidDataSource designDataSource;
	static DruidDataSource logDataSource;
	static DruidDataSource configDataSource;
	static JedisPool jedisPool;
	static Map<String, Object> config;

	public static void loadConfig() {
		{
			// 加载配置文件
			String path = "config/config.json";
			String jsonString = null;
			try {
				jsonString = FileUtils
						.readFileToString(new File(path), "UTF-8");
			} catch (IOException e) {
				e.printStackTrace();
			}
			config = JSON.parseObject(jsonString);
		}
		{
			// 初始逻辑库连接池
			logicDataSource = new DruidDataSource();
			Map<String, Object> sourceMap = SK_Map.getMap("LogicDataSource",
					config);
			logicDataSource.setUrl(SK_Map.getString("Url", sourceMap));
			logicDataSource.setUsername(SK_Map.getString("User", sourceMap));
			logicDataSource.setPassword(SK_Map.getString("Pass", sourceMap));
			logicDataSource.setDriverClassName(SK_Map.getString("Driver",
					sourceMap));
		}
		{
			// 初始生成库连接池
			designDataSource = new DruidDataSource();
			Map<String, Object> sourceMap = SK_Map.getMap("DesignDataSource",
					config);
			designDataSource.setUrl(SK_Map.getString("Url", sourceMap));
			designDataSource.setUsername(SK_Map.getString("User", sourceMap));
			designDataSource.setPassword(SK_Map.getString("Pass", sourceMap));
			designDataSource.setDriverClassName(SK_Map.getString("Driver",
					sourceMap));
		}
		{
			// 初始日志库连接池
			logDataSource = new DruidDataSource();
			Map<String, Object> sourceMap = SK_Map.getMap("LogDataSource",
					config);
			logDataSource.setUrl(SK_Map.getString("Url", sourceMap));
			logDataSource.setUsername(SK_Map.getString("User", sourceMap));
			logDataSource.setPassword(SK_Map.getString("Pass", sourceMap));
			logDataSource.setDriverClassName(SK_Map.getString("Driver",
					sourceMap));
		}
		{
			// 初始配置库连接池
			configDataSource = new DruidDataSource();
			Map<String, Object> sourceMap = SK_Map.getMap("ConfigDataSource",
					config);
			configDataSource.setUrl(SK_Map.getString("Url", sourceMap));
			configDataSource.setUsername(SK_Map.getString("User", sourceMap));
			configDataSource.setPassword(SK_Map.getString("Pass", sourceMap));
			configDataSource.setDriverClassName(SK_Map.getString("Driver",
					sourceMap));
		}
		{
			//初始redis
			jedisPool = new JedisPool("127.0.0.1");
		}
		SK_Config.init(logicDataSource, designDataSource, logDataSource,configDataSource, jedisPool);
	}
}
